package cn.community.care.module.system.controller.admin.shoppingcart;

import cn.community.care.module.system.controller.admin.shoppingcart.vo.ShoppingCartPageReqVO;
import cn.community.care.module.system.controller.admin.shoppingcart.vo.ShoppingCartRespVO;
import cn.community.care.module.system.controller.admin.shoppingcart.vo.ShoppingCartSaveReqVO;
import cn.community.care.module.system.dal.dataobject.shoppingcart.ShoppingCartDO;
import cn.community.care.module.system.service.shoppingcart.ShoppingCartService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.CommonResult;
import cn.community.care.framework.common.util.object.BeanUtils;
import static cn.community.care.framework.common.pojo.CommonResult.success;

import cn.community.care.framework.excel.core.util.ExcelUtils;

import cn.community.care.framework.operatelog.core.annotations.OperateLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.community.care.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 购物车")
@RestController
@RequestMapping("/system/shopping-cart")
@Validated
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/create")
    @Operation(summary = "创建购物车")
    public CommonResult<Long> createShoppingCart(@Valid @RequestBody ShoppingCartSaveReqVO createReqVO) {
        return success(shoppingCartService.createShoppingCart(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新购物车")
    public CommonResult<Boolean> updateShoppingCart(@Valid @RequestBody ShoppingCartSaveReqVO updateReqVO) {
        shoppingCartService.updateShoppingCart(updateReqVO);
        return success(true);
    }
    @PutMapping("/submit")
    @Operation(summary = "提交订单")
    public CommonResult<Boolean> submitShoppingCart(@Valid @RequestBody ShoppingCartSaveReqVO updateReqVO) {
        shoppingCartService.subShoppingCart(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除购物车")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteShoppingCart(@RequestParam("id") Long id) {
        shoppingCartService.deleteShoppingCart(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得购物车")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ShoppingCartRespVO> getShoppingCart(@RequestParam("id") Integer id) {
        ShoppingCartDO shoppingCart = shoppingCartService.getShoppingCart(id);
        return success(BeanUtils.toBean(shoppingCart, ShoppingCartRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得购物车分页")
    public CommonResult<PageResult<ShoppingCartRespVO>> getShoppingCartPage(@Valid @RequestBody ShoppingCartPageReqVO pageReqVO) {
        PageResult<ShoppingCartDO> pageResult = shoppingCartService.getShoppingCartPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShoppingCartRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出购物车 Excel")
    @PreAuthorize("@ss.hasPermission('app:shopping-cart:export')")
    @OperateLog(type = EXPORT)
    public void exportShoppingCartExcel(@Valid ShoppingCartPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShoppingCartDO> list = shoppingCartService.getShoppingCartPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "购物车.xls", "数据", ShoppingCartRespVO.class,
                        BeanUtils.toBean(list, ShoppingCartRespVO.class));
    }

}